﻿@namespace Bit.BlazorUI
@inherits BitComponentBase
@typeparam TItem

<div @ref="RootElement" @attributes="HtmlAttributes"
     id="@_Id"
     role="@Role"
     style="@StyleBuilder.Value"
     class="@ClassBuilder.Value"
     dir="@Dir?.ToString().ToLower()">
    @if (RowTemplate is not null)
    {
        if (Virtualize)
        {
            if (ItemsProvider is null || LoadMore)
            {
                <_BitBasicListVirtualize Items="_viewItems"
                                         ItemSize="@ItemSize"
                                         EmptyContent="EmptyContent"
                                         OverscanCount="@OverscanCount">
                    @RowTemplate(context)
                </_BitBasicListVirtualize>
            }
            else
            {
                <_BitBasicListVirtualize @ref="_bitBasicListVirtualizeRef"
                                         EmptyContent="EmptyContent"
                                         OverscanCount="@OverscanCount"
                                         Placeholder="VirtualizePlaceholder"
                                         ItemsProvider="ProvideVirtualizedItems">
                    @RowTemplate(context)
                </_BitBasicListVirtualize>
            }
        }
        else
        {
            if (_viewItems.Count > 0)
            {
                foreach (var item in _viewItems)
                {
                    @RowTemplate(item)
                }
            }
            else if (_isLoadingMore is false)
            {
                @EmptyContent
            }
        }
    }

    @if (LoadMore && _loadMoreFinished is false)
    {
        <button @onclick="() => PerformLoadMore(false)"
                style="@Styles?.LoadMoreButton"
                class="bit-bsl-lmb @Classes?.LoadMoreButton">
            @if (LoadMoreTemplate is not null)
            {
                @LoadMoreTemplate(_isLoadingMore)
            }
            else
            {
                <div style="@Styles?.LoadMoreText"
                     class="bit-bsl-lmt @Classes?.LoadMoreText">
                    @if (_isLoadingMore)
                    {
                        <span>...</span>
                    }
                    else
                    {
                        <span>@LoadMoreText</span>
                    }
                </div>
            }
        </button>
    }
</div>
